package net.asfun.jvalog.control;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.asfun.jvalog.entity.Paper;
import net.asfun.jvalog.service.CategoryService;
import net.asfun.jvalog.service.PostService;
import net.asfun.jvalog.util.RequestUtil;
import net.asfun.jvalog.util.ResponseUtil;
import net.asfun.jvalog.util.ValueFormater;
import net.asfun.jvalog.vo.Pager;
import net.asfun.jvalog.mvc.RequestMapping;

public class PostController {
	

	@RequestMapping("/post")
	public String index(HttpServletRequest req, Map<String, Object> model) {
		int page = RequestUtil.getIntParam(req, "page");
		Pager pager = new Pager(page);
		model.put("posts", PostService.listAll(pager));
		model.put("pager", pager);
		return "posts.html";
	}
	
	@RequestMapping("/post/create")
	public String create(HttpServletRequest req, Map<String, Object> model) {
		model.put("categories", CategoryService.listAll());
		return "post.html";
	}
	
	@RequestMapping("/post/add")
	public String add(HttpServletRequest req, Map<String, Object> model) {
		String title = RequestUtil.getRequiredParam(req, "title");
		String slogan =  RequestUtil.getStringParam(req, "slogan", title);
		String article = RequestUtil.getRequiredParam(req, "article");
		String tagnames = RequestUtil.getStringParam(req, "tagNames");
		String excerpt = RequestUtil.getStringParam(req, "excerpt");
		String source = RequestUtil.getStringParam(req, "source");
		String publish = RequestUtil.getStringParam(req, "publish");
		String date = RequestUtil.getStringParam(req, "date");
		Long[] categoryIds = RequestUtil.getLongParams(req, "categories");

		Paper paper = new Paper();
		paper.setType(Paper.POST_TYPE);
		paper.setContent(article);
		try {
			if ( date.length() == 0 ) {
				paper.setDate(new Date());
			} else {
				paper.setDate(parseDate(date));
			}
		} catch (Exception e) {
			paper.setDate(new Date());
		}
		paper.setExcerpt(excerpt);
		if ( publish.length() > 0 ) {
			paper.setPublished(true);
		}
		paper.setSlogan(slogan);
		paper.setSource(source);
		paper.setTitle(title);
		PostService.addPost(paper, tagnames, categoryIds);

		return ResponseUtil.feedback("post.add", req, model);
	}
	
	@RequestMapping("/post/delete")
	public String delete(HttpServletRequest req, Map<String, Object> model) {
		Long[] ids = RequestUtil.getLongParams(req, "id");
		for(int i=0; i<ids.length; i++) {
			PostService.delete(ids[i]);
		}
		return ResponseUtil.feedback("post.delete", req, model);
	}
	
	@RequestMapping("/post/update")
	public String update(HttpServletRequest req, Map<String, Object> model) {
		Long id = RequestUtil.getLongParam(req, "id");
		String title = RequestUtil.getRequiredParam(req, "title");
		String slogan =  RequestUtil.getStringParam(req, "slogan", title);
		String article = RequestUtil.getStringParam(req, "article");
		String tagnames = RequestUtil.getStringParam(req, "tagNames");
		String excerpt = RequestUtil.getStringParam(req, "excerpt");
		String source = RequestUtil.getStringParam(req, "source");
		String publish = RequestUtil.getStringParam(req, "publish");
		String date = RequestUtil.getStringParam(req, "date");
		Long[] categoryIds = RequestUtil.getLongParams(req, "categories");
		
		Paper paper = new Paper();
		paper.setId(id);
		paper.setTitle(title);
		paper.setSlogan(slogan);
		if ( article.length() > 0 ) {
			paper.setContent(article);
		}
		try {
			if ( date.length() > 0 )
				paper.setDate(parseDate(date));
		} catch (Exception e) {/*do not change*/}
		paper.setExcerpt(excerpt);
		paper.setSource(source);
		if ( publish.length() > 0 ) {
			paper.setPublished(true);
		} else {
			paper.setPublished(false);
		}
		PostService.update(paper, tagnames, categoryIds);
		
		return ResponseUtil.feedback("post.update", req, model);
	}
	
	@RequestMapping("/post/edit")
	public String edit(HttpServletRequest req, Map<String, Object> model) {
		Long id = RequestUtil.getLongParam(req, "id");
		model.put("post", PostService.getById(id));
		model.put("categories", CategoryService.listAll());
		return "post.html";
	}
	
	@RequestMapping("/post/publish")
	public String publish(HttpServletRequest req, Map<String, Object> model) {
		Long id = RequestUtil.getLongParam(req, "id");
		model.put("post", PostService.getById(id));
		model.put("categories", CategoryService.listAll());
		return "publish.html";
	}
	
	private Date parseDate(String date) {
		int len = date.length();
		if (len == 10 ) {
			date += ValueFormater.date2string(new Date(), " hh:mm:ss");
		}
		if (len == 13 ) {
			date += ValueFormater.date2string(new Date(), ":mm:ss");
		}
		if (len == 16 ) {
			date += ":00";
		}
		return ValueFormater.string2date(date, "yyyy-MM-dd HH:mm:ss");
	}
	
}
